package com.foxshooter;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;

public class StartWebApp {

	public static String host = "127.0.0.1";
	public static int port = 9999;
	public static String contextPath = "/";

	public static void start() throws Exception {
		StartWebUtil.init();

		Server jettyServer = new Server();
		Handler[] handlers = new Handler[2];

		WebAppContext context = new WebAppContext();
		context.setContextPath(contextPath);
		context.setWar("target/webapp");
		context.setDefaultsDescriptor("webdefault.xml");
		handlers[0] = context;

		handlers[1] = new DefaultHandler();
		HandlerCollection handler = new HandlerCollection();
		handler.setHandlers(handlers);
		jettyServer.setHandler(handler);

		Connector[] connectors = new Connector[1];
		Connector connector = new SelectChannelConnector();
		connector.setPort(port);
		connector.setHost(host);
		connector.setMaxIdleTime(0);
		connectors[0]=connector;

		jettyServer.setConnectors(connectors);
		jettyServer.start();

		System.out.println("http://" + host + ":" + port + contextPath);
	}

	public static void main(String[] args) throws Exception {
		start();
	}
}
